package com.skyworth.web;

import ch.qos.logback.access.jetty.RequestLogImpl;
import ch.qos.logback.access.servlet.TeeFilter;
import com.skyworth.web.common.form.MenuCss;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.NCSARequestLog;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.HandlerCollection;
import org.eclipse.jetty.server.handler.RequestLogHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;
import org.springframework.boot.context.embedded.FilterRegistrationBean;
import org.springframework.boot.context.embedded.MultipartConfigFactory;
import org.springframework.boot.context.embedded.jetty.JettyEmbeddedServletContainerFactory;
import org.springframework.boot.context.embedded.jetty.JettyServerCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

import javax.servlet.MultipartConfigElement;

/**
 * Created by holin on 11/22/15.
 */
@Configuration
public class WebConfig {

    @Autowired
    private Environment env;

    @Bean
    public MultipartConfigElement multipartConfigElement(){
        MultipartConfigFactory multipartConfigFactory = new MultipartConfigFactory();
        multipartConfigFactory.setMaxFileSize(env.getProperty("multipart.max-file-size"));
        multipartConfigFactory.setMaxRequestSize(env.getProperty("multipart.max-request-size"));
        return multipartConfigFactory.createMultipartConfig();
    }


    @Bean
    public MenuCss menuCss() {
        return new MenuCss();
    }

    @Bean
    public FilterRegistrationBean teeFilter() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        filterRegistrationBean.setFilter(new TeeFilter());
        return filterRegistrationBean;
    }

    @Bean
    public EmbeddedServletContainerFactory jettyConfigBean() {
        JettyEmbeddedServletContainerFactory jef = new JettyEmbeddedServletContainerFactory();
        jef.addServerCustomizers(new JettyServerCustomizer() {
            public void customize(Server server) {
                HandlerCollection handlers = new HandlerCollection();
                for (Handler handler : server.getHandlers()) {
                    handlers.addHandler(handler);
                }
                RequestLogHandler reqLogs = new RequestLogHandler();
                NCSARequestLog reqLogImpl = new NCSARequestLog("./logs/access-yyyy_mm_dd.log");

                RequestLogImpl requestLogImpl = new ch.qos.logback.access.jetty.RequestLogImpl();
                requestLogImpl.setResource("/logback-access.xml");
                requestLogImpl.setQuiet(false);


                reqLogImpl.setRetainDays(30);
                reqLogImpl.setAppend(true);
                reqLogImpl.setExtended(false);
                reqLogImpl.setLogTimeZone("GMT");
                reqLogs.setRequestLog(requestLogImpl);
                handlers.addHandler(reqLogs);
                server.setHandler(handlers);

            }
        });
        return jef;
    }

}